package com.dosh;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.database.Cursor;
/**
 * The Planning class describes an entry in the Planning table.
 * A Planning entry is a Booking with date when it's created
 * All fields are necessary for a table entry.
 */
public class Planning extends Activity{
	/**
	 * The ID of the Planning entry
	 */
	public long planning_ID;
	/**
	 * The start Time of this Planning
	 */
	public Date start;
	/**
	 * The interval for the reputation of the jobs
	 */
	public Cycle cycle;
	/**
	 * The Budget in which the jobs should be booked
	 */
	public Budget budget;
	/**
	 * A reference to the related Booking
	 */
	public Booking booking;
	/**
	 * The number of reputations
	 */
	public int count;
	
	/**
	 * This Constructor is used, if there is no entry in the database.
	 * @param dba A reference to the DBAdapter
	 */
	public Planning(Date start, Cycle cycle, Budget budget,
			Booking booking, int count, DBAdapter dba) {
		
		this.start = start;
		this.cycle = cycle;
		this.budget = budget;
		this.booking = booking;
		this.count = count;
		
		dba.open();
		this.planning_ID = dba.insertPlanning(budget.budget_ID, booking.booking_ID, cycle.cycle_ID, ""+start.getDate()+"."+start.getMonth()+"."+start.getYear(), count);
		dba.close();
		this.toJob(dba);
	}
	/**
	 * This Constructor is used, if there an entry already exists in the database.
	 * @param dba A reference to the DBAdapter
	 */
	public Planning(int planning_ID, DBAdapter dba) {
		
		this.planning_ID = planning_ID;
		dba.open();
		Cursor pCursor = dba.SELECTbyId("Planning", planning_ID);
		dba.close();
		String[] helpdate = new String[3];
		helpdate = pCursor.getString(4).split("\\.");
		this.start = new Date(Integer.parseInt(helpdate[2]), Integer.parseInt(helpdate[1]), Integer.parseInt(helpdate[0]));
		this.cycle = MainActivity.cycles[(int)pCursor.getLong(3)];
		this.budget = new Budget (pCursor.getLong(2), dba);
		this.booking = new Booking (pCursor.getLong(1), dba);
	}
	/**
	 * This method updates the table entry
	 * @param dba A reference to the DBAdapter
	 * @return gives information,if the update was successful
	 */
	public boolean update(DBAdapter dba) 
	{
		boolean bool = true;
		
		dba.open();
		bool = dba.update("Planning", this.planning_ID, "Starttime", ""+this.start.getDate()+this.start.getMonth()+this.start.getYear());
		bool = dba.update("Planning", this.planning_ID, "Count", this.count);
		bool = dba.update("Planning", this.planning_ID, "Cycle", this.cycle.cycle_ID);
		bool = dba.update("Planning", this.planning_ID, "Budget", this.budget.budget_ID);
		bool = dba.update("Planning", this.planning_ID, "Booking", this.booking.booking_ID);
		dba.close();
		
		return bool;
	}
	/**
	 * This method creates all job, which are created from this Planning
	 * @param dba A reference to the DBAdapter
	 */
	public void toJob (DBAdapter dba){
		Job[] jobs = new Job[this.count+1];
		Date start = this.start;
		Calendar calendar = new GregorianCalendar();
		jobs[0]= new Job(start, this.booking.amount, this, dba);
		if(this.count !=0){
			for(int i = 1; i<=this.count; i++){	
				System.out.println("help3");
				calendar.setTime(start);
					if(this.cycle.name.equals("day(s)") || this.cycle.name.equals("Tag(e)")){
						calendar.add(Calendar.DAY_OF_MONTH, this.cycle.interval);
					}else if(this.cycle.name.equals("week(s)") || this.cycle.name.equals("Woche(n)")){
						calendar.add(Calendar.WEEK_OF_MONTH, this.cycle.interval);
					}else if(this.cycle.name.equals("month(s)") || this.cycle.name.equals("Monat(e)")){
						calendar.add(Calendar.MONTH, this.cycle.interval);
					}else if(this.cycle.name.equals("year(s)") || this.cycle.name.equals("Jahre(e)")){
						calendar.add(Calendar.YEAR, this.cycle.interval);
					}
				start = calendar.getTime();
				jobs[i]= new Job(start, this.booking.amount, this, dba);
			}
		}
		
	}

}
